﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.UI.Xaml.Navigation;
using AudioBooks.Data;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using AudioBooks.BookDetailsGroup;
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
using AudioBooks.SQLite;

namespace AudioBooks
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class Main : AudioBooks.Common.LayoutAwarePage
    {

        bool downloadedFlag = false;

        public Main()
        {
            this.InitializeComponent();
            CallServices();
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
           
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            
            var bookmark = HandlePlayChapter.GetDataPlayChapterInfos();
            if (bookmark == null || bookmark.Count == 0)
            {
                bookmarkHub.Visibility = Visibility.Collapsed;
            }
            else
            {
                bookmarkHub.Visibility = Visibility.Visible;
               this.DefaultViewModel["Bookmarks"] = bookmark.OrderByDescending(item => item.KeyMark).Take(10).ToList();
            }
            base.OnNavigatedTo(e);
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }


        private async void CallServices()
        {
            var getSeriesBookDetail = await App.CallService.GetSeriesBookDetailAsync("27");
            if (getSeriesBookDetail.book_list == null)
            {
                MessageDialog mess = new MessageDialog("Services got problem please try again later!!");
                await mess.ShowAsync();
                Window.Current.Close();
                return;
            }
            StaticData.bookList.Clear();
            foreach (var data in getSeriesBookDetail.book_list)
            {
                Book book = new Book() { bookId = data.book_id, _imagePath = data.cover_link ,title = data.book_name,shortDescrip = data.author};
                StaticData.bookList.Add(book);
            }
            this.DefaultViewModel["Books"] = StaticData.bookList;

        }
 
        private void ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = (Book)e.ClickedItem;
            this.Frame.Navigate(typeof(ItemPage), item.bookId);
        }

        private void GridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = (PlayChapterInfo)e.ClickedItem;
            this.Frame.Navigate(typeof(ItemPage), item);
        }

       
    }
}
